
package com.plian.system.service.fr.adjustment.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.dao.fr.budgetadjustment.BudgetAdjustmentDao;
import com.plian.system.entity.fr.adjustment.AdjustmentTableConfig;
import com.plian.system.mapper.fr.adjustment.AdjustmentTableConfigMapper;
import com.plian.system.service.fr.adjustment.IAdjustmentTableConfigService;
import com.plian.system.service.fr.adjustment.IAdjustmentTableVersionService;
import com.plian.system.vo.fr.adjustment.AdjustmentTableConfigVO;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 预算调整表格配置 前端用 服务实现类
 *
 * @author jianglei
 * @since 2021-09-14
 */
@Service
@AllArgsConstructor
public class AdjustmentTableConfigServiceImpl extends ServiceImpl<AdjustmentTableConfigMapper, AdjustmentTableConfig> implements IAdjustmentTableConfigService {

    private IAdjustmentTableVersionService adjustmentTableVersionService;

    private BudgetAdjustmentDao budgetAdjustmentDao;

    /**
     * logger属性.
     */
    private static final Logger LOGGER  = LoggerFactory.getLogger(AdjustmentTableConfigServiceImpl.class);


    @Override
    public AdjustmentTableConfigVO getTableConfig(String year) {
        AdjustmentTableConfig reportTableConfig = getOne(new QueryWrapper<AdjustmentTableConfig>().eq("year", year));
        List<PageData> list = new ArrayList<>();
        try {
            PageData pd = new PageData();
            pd.put("version", adjustmentTableVersionService.getVersion(year));
            list = budgetAdjustmentDao.listAllOrg(pd);
        }catch (Exception e){
            LOGGER.error("" + e);
        }

        AdjustmentTableConfigVO vo = AdjustmentTableConfigVO.builder()
                .tableConst(JSONArray.parseArray(reportTableConfig.getTableConst()))
                .tableNames(JSON.parseArray(reportTableConfig.getTableNames()))
                .colNames(JSON.parseArray(reportTableConfig.getColNames()))
                .required(JSON.parse(reportTableConfig.getRequired()))
                .rule(JSON.parseArray(reportTableConfig.getRule()))
                .reportOrgList(list)
                .build();

        return vo;
    }

}
